using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace Doc2XPS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Write to the log that MOSS passes in, happens to be C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS
            ParseCommandLine(args);
            gLog = new StreamWriter(gLogPath, true);

            try
            {
                gLog.WriteLine(DateTime.Now.ToString() + " Started");
                gLog.WriteLine(Environment.CommandLine);

                ConvertDoc2Pdf(gInFileName, gOutFileName);
            }
            catch (Exception e)
            {
                gLog.WriteLine(e.Message);
                Environment.ExitCode = 100;
            }
            finally
            {
                gLog.Close();
            }  
        }

        private static void ParseCommandLine(string[] args)
        {
            int i = 0;
            while (i < args.Length)
            {
                string s = args[i];
                switch (s.ToLower())
                {
                    case "-in":
                        i++;
                        gInFileName = args[i];
                        break;
                    case "-out":
                        i++;
                        gOutFileName = args[i];
                        break;
                    case "-config":
                        // Skip the name of the config file and do nothing.
                        i++;
                        break;
                    case "-log":
                        i++;
                        gLogPath = args[i];
                        break;
                    default:
                        throw new Exception("Unknown command line argument: " + s);
                }
                i++;
            }
        }
        private static void ConvertDoc2Pdf(string inFileName, string outFileName)
        {
            ApplicationClass wordApplication = new ApplicationClass();
            Document wordDocument = null;

            object paramSourceDocPath = inFileName;
            object paramMissing = Type.Missing;

            string paramExportFilePath = outFileName;
            WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport = false;
            WdExportOptimizeFor paramExportOptimizeFor =
                WdExportOptimizeFor.wdExportOptimizeForPrint;
            WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = false;
            WdExportCreateBookmarks paramCreateBookmarks =
                WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;

            try
            {
                // Open the source document.
                wordDocument = wordApplication.Documents.Open(ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing);

                // Export it in the specified format.
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }
            catch (Exception e)
            {
                gLog.WriteLine(e.Message);
                Environment.ExitCode = 100;
            }
            finally
            {
                // Close and release the Document object.
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordDocument = null;
                }

                // Quit Word and release the ApplicationClass object.
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }


        }

        private static string gInFileName;
        private static string gOutFileName;
        private static string gLogPath;
        private static StreamWriter gLog;
    }
}
